package cn.antia.db;

import cn.antia.cep.common.AntiaLog;
import cn.antia.cwin.SysConf;

import java.util.ResourceBundle;

/**
 * User: Xiao Jacob
 * Date: 2008-7-14
 * Time: 15:43:07
 */
public class DBJavaTypeMap {
private static ResourceBundle rb = ResourceBundle.getBundle("cn.antia.cep.db.typemap");

        /***
         *
         * @param dbType
         * @param rs 返回参数(字符串数组(第一个为长度，第二个为最小值，第三个为最大值))
         * @return
         */
        public static String getJavaType(String dbType,String[] rs){
            String value = null;
            try{
                String key = SysConf.SYSTEM_DB + "." + dbType.toUpperCase();
                value = rb.getString(key);
                String an,av;
                if(value != null && value.trim().length() > 0){
                    value = value.trim();
                    String[] vs = value.split(";");
                    if(vs.length == 1){
                        return vs[0];
                    }else{
                        value = vs[0].trim();
                        for(int i = 1;i<vs.length;i++){
                            String[] vs1 = vs[i].split(":");
                            an = vs1[0];
                            av = vs1[1];
                            if(an.equals("len")){
                                rs[0] = av;
                            }else if(an.equals("min")){
                                rs[1] = av;
                            }else if(an.equals("max")){
                                rs[2] = av;
                            }
                        }
                    }
                }
            }catch(Exception e){
                AntiaLog.error("db type mapping config error: db type:" + dbType);
            }
            return value;
        }

        public static void main(String[] args){
            DBJavaTypeMap map = new DBJavaTypeMap();
            String[] res = new String[]{"","",""};
            String jt = map.getJavaType("TINYINT",res);
            System.out.println("jtype:" + jt);
            System.out.println("rs:" + res);
            String s = "char(980)";
            System.out.println(s.substring(s.indexOf("(") + 1,s.indexOf(")")));
        }
}
